Ext.define('hkyg.controller.LoginWin', {
	extend: 'Ext.app.Controller',
	views: ['LoginWin','NewsPanel'],
	refs: [{ref: 'NewsPanel', selector: 'newsPanel'}],
	init: function() {
		 var me = this;
		 if(me.isInited) {
			return;
	     }
		 me.control({
			 'loginWin': {
				loginSuccess: function() {
					Ext.widget('newsPanel');
				}
			 },
			 'newsPanel button': {
			 	click: me.newsButtonClick
			 },
			 'newsPanel grid':{
				 select: me.newsSelect,
				 afterrender: function(scope) {
					 scope.getStore().loadPage(1);
				 }
			 }
		 });
		 me.isInited = true;
    },
    newsButtonClick: function(scope) {
    	switch(scope.itemId) {
    		case 'addNews': this.addNews();break;
    		case 'delNews': this.delNews(); break;
    		case 'searchBtn': this.searchNews();break;
    		case 'saveNews': this.saveNews();break;
    	}
    },
    addNews: function() {
    	var me = this;
    	var newsPanel = me.getNewsPanel();
    	var rec = Ext.create('hkyg.model.WebContent');
    	newsPanel.newsStore.insert(0,rec);
    	newsPanel.newsStore.commitChanges();
    	var grid = newsPanel.down('grid');
    	if(grid) {
    		grid.getSelectionModel().select(0);
    	}
    },
    delNews: function() {
    	var me = this;
    	var newsPanel = me.getNewsPanel();
    	var grid = newsPanel.down('grid');
    	var selModel = grid.getSelectionModel();
    	var selection = selModel.getSelection();
    	if(selection.length > 0) {
    		var rec = selection[0];
    		Ext.Msg.confirm('提示','你确定要删除[ '+rec.get('title')+' ]吗?',
		     	function fn(id){
					if(id==Ext.Msg.buttonIds[1]){
						Ext.Ajax.request({
			    			url: 'hkyg/Background.do?method=delNews',
			    			params: rec.getData(),
			    			success: function() {
			    				newsPanel.cleanEdtPanel();
			    				newsPanel.newsStore.loadPage(1);
			    			}
			    		});
					}
			});
    		
    	}else {
    		Ext.Msg.alert('提示', '请选择一条记录再操作。');
    	}
    },
    searchNews: function() {
    	var me = this;
    	var newsPanel = me.getNewsPanel();
    	newsPanel.doSearch();
    },
    saveNews: function() {
    	var me = this;
    	var newsPanel = me.getNewsPanel();
    	var titleFld = newsPanel.down('textfield[itemId=title]');
    	if(titleFld.getValue().trim() == '') {
    		Ext.Msg.alert('提示', '标题不允许为空');
    		return;
    	}
    	var contentFld = newsPanel.down('htmleditor');
    	params = {
    			title: titleFld.getValue(),
    			content: contentFld.getValue()
		};
    	var url = 'hkyg/Background.do?method=';
    	if(newsPanel.lastRec && newsPanel.lastRec.get('content_id') == 0) {
    		url += 'addNews';
    	} else {
    		Ext.apply(params,{content_id: newsPanel.lastRec.get('content_id')});
    		url += 'updateNews';
    	}
    	Ext.Ajax.request({
    		url: url,
    		params: params,
    		success: function() {
    			delete newsPanel.newsStore.proxy.extraParams.query;
    			newsPanel.newsStore.loadPage(1);
    		}
    	});
    },
    //选择新闻列表上的记录
    newsSelect: function(rowModel, rec) {
    	var me = this;
    	var newsPanel = me.getNewsPanel();
    	newsPanel.lastRec = rec;
    	var titleFld = newsPanel.down('textfield[itemId=title]');
    	var contentFld = newsPanel.down('htmleditor');
    	titleFld.setValue(rec.get('title'));
    	contentFld.setValue(rec.get('content'));
    }
});